home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6974 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtuals in constructor
  5. Date: Wed, 21 Feb 1996 01:50:03 GMT
  6. Organization: Netcom
  7. Message-ID: <312a779d.5728997@nntp.ix.netcom.com>
  8. References: <312A3A72.688D@scopus.ch>
  9. NNTP-Posting-Host: ix-dc13-30.ix.netcom.com
  10. X-NETCOM-Date: Tue Feb 20  5:50:15 PM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. Fabienne Guinnard <guinnard_f@scopus.ch> wrote:
  14.  
  15. > Hi,
  16. > does anyone know why the following code doesn't work ?
  17. > Run-time error: pure virtual function called
  18. > class A
  19. > {
  20. >   public:
  21. >     A(VOID) { Method(); }
  22. >     virtual VOID Method(VOID) = 0;
  23. > };
  24. > class B : public A
  25. > {
  26. >   public:
  27. >     B(VOID) {}
  28. >     VOID Method(VOID) { MessageBox(NULL, "", "", MB_OK); }
  29. > };
  30. > int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  31. > {
  32. >     B b;
  33. >     return 0;
  34. > }
  35.  
  36. Because you are calling a pure virtual function.
  37.  
  38. When A::A() is invoked to construct the A part of b the B part does
  39. not yet exist.  The call to Method() in A::A() therefore calls
  40. A::Method() which is not defined.
  41.  
  42.  
  43. Michael M Rubenstein
  44.